<?php
namespace common\modules\storage;

use common\core\modularity\ModuleServiceInterface;
use common\modules\storage\models\KeyStorageItem;

class StorageService implements ModuleServiceInterface
{
    public function getServiceId()
    {
        return 'storageService';
    }
    
    public function loadConfig($key)
    {
        return KeyStorageItem::getModel($key);
    }
    
    public function getConfigValue($key)
    {
        return KeyStorageItem::getValue($key);
    }
    
    public function saveConfig($key, $value)
    {
        $item = KeyStorageItem::getModel($key);
        if ($item) {
            $item->key = $key;
            $item->value = $value;
            $item->save();
        }
        else {
            $item = new KeyStorageItem();
            $item->key = $key;
            $item->value = $value;
            $item->save();
        }
        KeyStorageItem::clearCachedConfig($key);
    }
}